'use strict';

const Controller = require('../core/base_controller');

class Test extends Controller {

	async index() {

		const { app } = this;

		const { sqlstr } = ctx.request.body;

		const sqljson = JSON.parse(sqlstr);

		// const sqljson = {
		// 	col : [
		// 		{
		// 			tname : 'dlq_menu',
		// 			cname : 'aa',
		// 			asname : '',
		// 		},
		// 		{
		// 			tname : 'dlq_menu',
		// 			cname : 'vv',
		// 			asname : 'ss',
		// 		},
		// 	],

		// 	table : {
		// 		master : 'dlq_menu',
		// 		condition : [
		// 			{
		// 				what : ' left join ',
		// 				table : 'dlq_role_menu',
		// 				on : ['id', 'menu_id'],
		// 				// and : [
		// 				// 	{
		// 				// 		tname : 'dlq_role_menu',
		// 				// 	}
		// 				// ]
		// 			}
		// 		]
		// 	},

		// 	where : [
		// 		{
		// 			tname : 'dlq_menu',
		// 			cname : 'pid',
		// 			what : ' = ',
		// 			value : '?',
		// 			condition : ' and ',
		// 		},
		// 		{
		// 			tname : 'dlq_role_menu',
		// 			cname : 'checked',
		// 			what : ' = ',
		// 			value : '1',
		// 			condition : ' and ',
		// 		}
		// 	]
		// };

		let sql = ' select ';

		let col = '';
		sqljson.col.forEach(item => {
			col += item.tname + '.' + item.cname;
			if(item1.asname != '') col += ' as ' + item.asname;
			col += ' ,';
		})

        col = col.replace(/,$/gi,"");

		let table = ' from ' + sqljson.table.master;
		sqljson.table.condition.forEach(item => {
			table += item.what + item.table + ' on ' + 
			sqljson.table.master + '.' + item.on[0] + ' = ' +
			item.table + '.' + item.on[1] + ' ';
		});

		let where = ' where ';

		sqljson.where.forEach((item, index) => {
			const { tname, cname, what, value, condition } = item;
			if(index != 0) where += condition;
			where += tname + '.' + cname + what + value;
		});

		this.success(sql + col + table + where);
	}

	async test () {

		const { app, ctx } = this;

		const {code} = ctx.query;

		console.log(code);

		let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=wx3dfaa83570a07a31&secret=71e4a4fe68f38910444578e606caf807&js_code='+code+'&grant_type=authorization_code'

		const result = await ctx.curl(url, { dataType: 'json' });

		this.success(result);
	}



	async getSql () {

		const { app, ctx } = this;

		const { sqlstr } = ctx.request.body;

		const sqljson = JSON.parse(sqlstr);

		let sql = ' select ';

		let col = '';
		sqljson.col.forEach(item => {
			col += item.tname + '.' + item.cname;
			if(item.asname != '') col += ' as ' + item.asname;
			col += ' ,';
		})

        col = col.replace(/,$/gi,"");

		let table = ' from ' + sqljson.table.master;
		sqljson.table.condition.forEach(item => {
			table += item.what + item.table + ' on ' + 
			sqljson.table.master + '.' + item.on[0] + ' = ' +
			item.table + '.' + item.on[1] + ' ';
		});

		let where = ' where ';

		sqljson.where.forEach((item, index) => {
			const { tname, cname, what, value, condition } = item;
			if(index != 0) where += condition;
			where += tname + '.' + cname + what + value ;
		});

		this.success(sql + col + table + where);
	}

}

module.exports = Test;
